package com.bluesky.controller;

import com.bluesky.service.lesson007.Lesson007Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/lesson007")
@Slf4j
public class Lesson007Controller {

    @Autowired
    private Lesson007Service lesson007Service;

    /**
     * 声明式事务，事务范围比较大
     * @return
     */
    @GetMapping("/bigTransaction")
    public boolean bigTransaction(){
        try{
            lesson007Service.bigTransaction();
            return true;
        }catch (Exception e){
            log.error("声明式事务 执行异常:{}", e.getMessage());
            return false;
        }
    }

    /**
     * 使用 TransactionTemplate 编程式事务，可以灵活的控制事务的范围
     * @return
     */
    @GetMapping("/smallTransaction")
    public boolean smallTransaction(){
        try{
            lesson007Service.smallTranscation();
            return true;
        }catch (Exception e){
            log.error("编程式事务 执行异常:{}", e.getMessage());
            return false;
        }
    }
}
